package com.fuelwallet.view;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

import com.fuelwallet.R;
import com.fuelwallet.model.AccountListAdapter;
import com.fuelwallet.model.CarProfileHandle;
import com.fuelwallet.model.HistoryHandle;

public class CarProfileManagerActivity extends ListActivity {
    /** Called when the activity is first created. */
	
	private CarProfileHandle profiles;
	private HistoryHandle historys;
	private AccountListAdapter adapter;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.car_profile_manager);
        setTitle("Account Manager");
        
        profiles = new CarProfileHandle(this);
        historys = new HistoryHandle(this);
        updateAdapter();
        //setListAdapter(adapter);
    }
    
    public void onBackPressed(){		
		super.onBackPressed();
		overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);
	}
    
    private void updateAdapter(){
    	profiles.open();
    	List<String> values = profiles.getAccountNameList();
        List<String> descriptions = new ArrayList<String>();
        for(int i=0;i<values.size();i++){
        	descriptions.add(profiles.getCarProfile(values.get(i)).getFuelName()
        			+ " (" + profiles.getCarProfile(values.get(i)).getDisplacement() + "L)");
        }
        
        adapter = new AccountListAdapter(this,R.layout.manager_listitem,values,descriptions);
        profiles.close();
        setListAdapter(adapter);
        CheckAdapter();
    }
    
    
    private void CheckAdapter(){
    	if(adapter.isEmpty())
    		alertNameListEmpty();
    }
    
    private void alertNameListEmpty(){
    	Builder MyAlertDialog = new AlertDialog.Builder(this);
    	MyAlertDialog.setTitle("No profile exists");
    	MyAlertDialog.setMessage("Please create a new car profile first.");
    	DialogInterface.OnClickListener OkClick = new DialogInterface.OnClickListener()
    	{
    		public void onClick(DialogInterface dialog, int which) {
    			dialog.cancel();
    			Intent intent = new Intent(CarProfileManagerActivity.this, AddProfileActivity.class );
    	    	Bundle bundle = new Bundle(); 
    	    	bundle.putString("account_name", "");
    	    	intent.putExtras(bundle);
    			startActivity(intent);
    		}
    	};
    	MyAlertDialog.setNeutralButton("OK", OkClick);
    	MyAlertDialog.show(); 	
    }
    
    @Override  
    protected void onListItemClick(ListView l, View v, int position, long id) {  
    	
    	String account_name = l.getItemAtPosition(position).toString();
    	alertAccountManager(account_name);
      
		super.onListItemClick(l, v, position, id);
    }
    
    private void alertAccountManager(final String account_name){
    	final CharSequence[] items = {"Edit", "Delete", "Clear History", "Create new account"};

    	AlertDialog.Builder builder = new AlertDialog.Builder(this);
    	builder.setTitle(account_name);
    	builder.setItems(items, new DialogInterface.OnClickListener() {
    	    public void onClick(DialogInterface dialog, int item) {
    	    	switch(item){
    	    		case 0:
    	    			dialog.cancel();
    	    			Intent intent = new Intent(CarProfileManagerActivity.this, AddProfileActivity.class );
    	    	    	Bundle bundle = new Bundle(); 
    	    	    	bundle.putString("account_name", account_name);
    	    	    	intent.putExtras(bundle);
    	    			startActivity(intent);
    	    			break;
    	    		case 1:
    	    			dialog.cancel();
    	    			profiles.open();
    	    			historys.open();
    	                	profiles.deleteCarProfile(account_name);
    	                	historys.deleteHistory(account_name);
    	                historys.close();
    	                profiles.close();
    	                Toast.makeText(getApplicationContext(), 
    	                		account_name + " has been deleted! ", 
    	                		Toast.LENGTH_SHORT).show();
    	                updateAdapter();
    	                
    	                break;
    	    		case 2:
    	    			dialog.cancel();
    	    			clearHistory(account_name);
    	    			Toast.makeText(getApplicationContext(), 
    	                		"The history of "+ account_name + " has been cleared! ", 
    	                		Toast.LENGTH_SHORT).show();
    	    			break;
    	    		case 3:
    	    			dialog.cancel();
    	    			Intent intent2 = new Intent(CarProfileManagerActivity.this, AddProfileActivity.class );
    	    	    	Bundle bundle2 = new Bundle(); 
    	    	    	bundle2.putString("account_name", "");
    	    	    	intent2.putExtras(bundle2);
    	    			startActivity(intent2);
    	    			overridePendingTransition(android.R.anim.slide_in_left,android.R.anim.slide_out_right);    	    			
    	    			break;
    	    	}
    	    }
    	});
    	AlertDialog alert = builder.create(); 	
    	alert.show();
    }
    
    public void clearHistory(final String account_name){
    	HistoryHandle history = new HistoryHandle(this);
    	history.open();
    	history.deleteHistory(account_name);
    	history.close();
    }
    
    @Override
	protected void onResume() {
    	updateAdapter();
		super.onResume();
	}
}